package com.neusoft.yuejian_01.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.UserBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.UserModel;



public class RegActivity extends BaseActivity implements RetrofitListener {
    private Button btn_reg;
    private EditText et_username, et_userpass, et_mobilenum, et_again_userpass;
    private GoogleApiClient client;

//连接xml布局控件
    protected void initView() {
        setContentView(R.layout.activity_reg);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        et_again_userpass = (EditText) findViewById(R.id.again_password);
        et_mobilenum = (EditText) findViewById(R.id.mobilenum);
        et_userpass = (EditText) findViewById(R.id.editText_password);
        et_username = (EditText) findViewById(R.id.username);
        setListener();
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

//判断是否注册成功
     protected void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = et_username.getText().toString();
                String password = et_userpass.getText().toString();
                String password_again =et_again_userpass.getText().toString();
                UserModel userModel = new UserModel();
               userModel.reg(username,password, password_again,RegActivity.this);
            }
        });
    }

    @Override
    protected void initData() {

    }

    //对用户输入的信息进行判断，两次密码是否相同
    private void setListener() {

        et_userpass.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus){
                    Toast.makeText(RegActivity.this,"密码由7为数字和英文字母组成",Toast.LENGTH_SHORT).show();
                }
                else {
                    //失去焦点处理
                }
            }
        });
        et_mobilenum.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus) {
                    String pwd = et_userpass.getText().toString();
                    String again = et_again_userpass.getText().toString();
                    if (!pwd.equals(again)) {
                        Toast.makeText(RegActivity.this, "请重新填写，密码前后不一致", Toast.LENGTH_SHORT).show();

                    }
                    if (pwd.equals(again)){

                        Toast.makeText(RegActivity.this, "密码一致，请记住密码", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    //失去焦点处理
                }
            }
        });
    }
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Reg Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
//判断注册是否成功，判断输入的两次密码是否相同
    @Override
    public void onSuccess(Object object, int flag) {
        final String username = et_username.getText().toString();
        String password = et_userpass.getText().toString();
        String password_again =et_again_userpass.getText().toString();
        if(!password.equals("")&&password .equals(password_again)) {
            UserBean userBean = (UserBean) object;
            String success = userBean.getSuccess();
            if ("0".equals(success)) {
                Toast.makeText(getApplication(), "注册失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplication(), "注册成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(RegActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
         }
          else{
                Toast.makeText(RegActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
            }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();
    }
}

